package com.eii.system.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.eii.common.web.controller.BaseController;
import com.eii.common.web.domain.Result;
import com.eii.common.web.page.PageResult;
import com.eii.excel.util.ExcelUtils;
import com.eii.log.annotation.AuditLog;
import com.eii.oauth2.common.annotation.InnerAuth;
import com.eii.oauth2.common.annotation.RequiresPermissions;
import com.eii.system.controller.vo.SysLogininforExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.eii.system.api.domain.SysLogininfor;
import com.eii.system.service.ISysLogininforService;

/**
 * 系统访问记录
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController extends BaseController {

    private final static String CONTROLLER_TITLE = "登录日志";

    @Autowired
    private ISysLogininforService logininforService;

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public PageResult list(SysLogininfor logininfor) {
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getPageResult(list);
    }

    @AuditLog(title = CONTROLLER_TITLE, operation = "导出")
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) throws IOException {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        List<SysLogininforExcel> sysLogininforExcels = BeanUtil.copyToList(list, SysLogininforExcel.class);
        String datestr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String filename = "登录日志" + datestr + ".xlsx";
        ExcelUtils.exportExcelStream(response, filename, SysLogininforExcel.class, sysLogininforExcels);
    }

    @RequiresPermissions("system:logininfor:remove")
    @AuditLog(title = CONTROLLER_TITLE, operation = "'删除:' + #infoIds")
    @DeleteMapping("/{infoIds}")
    public Result remove(@PathVariable Long[] infoIds) {
        return toResult(logininforService.deleteLogininforByIds(infoIds));
    }

    @RequiresPermissions("system:logininfor:remove")
    @AuditLog(title = CONTROLLER_TITLE, operation = "清空系统登录日志")
    @DeleteMapping("/clean")
    public Result clean() {
        logininforService.cleanLogininfor();
        return Result.success();
    }

    @InnerAuth
    @PostMapping
    public Result add(@RequestBody SysLogininfor logininfor) {
        return toResult(logininforService.insertLogininfor(logininfor));
    }
}
